﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using YT.Commons;
using YT.DB;
using YT.Modules;

namespace YT.XH.SuddenDeafness
{
    public partial class frmSDTreatmentEffectTitle_28 : Form
    {
        public frmSDTreatmentEffectTitle_28()
        {
            InitializeComponent();
            mstrSickRecID = SysVar.SickRecID;
            mstrSeqNo = "3";
        }
        private string mstrSickRecID = string.Empty;
        private FormStatus mfrmStatus;
        private string mstrTableName = "TreatmentEffectHdr";
        private string mstrSeqNo = string.Empty;
        ArrayList marrlist = new ArrayList();
        
       
        public frmSDTreatmentEffectTitle_28(string strSickRecID, FormStatus frmStatus)
        {
            InitializeComponent();           
            mstrSickRecID = strSickRecID;
            mfrmStatus = frmStatus;
        }
        public frmSDTreatmentEffectTitle_28(string strSickRecID, FormStatus frmStatus, string strExamSeqNo  )
        {
            InitializeComponent();
            mstrSeqNo = strExamSeqNo; ;
            mstrSickRecID = strSickRecID;
            mfrmStatus = frmStatus;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }

        private void FindPanel(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                if (con.GetType().Name == "Panel" && con.Tag != "" && con.Tag != null)
                {
                    marrlist.Add(con);
                }
                if (con.Controls.Count > 0)
                {
                    FindPanel(con);
                }
                if (con.GetType().Name == "TextBox") con.KeyDown += new System.Windows.Forms.KeyEventHandler(this.SelectNextText); 
            }
        }
        private void SetControl()
        {
            string strSQL = "select * from TreatmentEffectHdr where SeqNo='" + mstrSeqNo + "' and Type='" + TitleType.SuddenDeafness + "' and SickRecID='" + mstrSickRecID + "'";
            DBAccess dbcon = new DBAccess(false, false);
            DataRow drRow = dbcon.GetDataFirstRow(strSQL);
            if (drRow != null)
            {
                panel2.Enabled = false;
            }
            dbcon.CloseCon();
        }
        private void FillData()
        {
            string strTableName = "TreatmentEffectResult";
            DBAccess dbcon = new DBAccess(false, false);
            foreach (Control cons in this.Controls)
            {
                FindPanel(cons);
            }
            SetControl();
            foreach (Control conDtl in marrlist)
            {
                if (conDtl.GetType().Name == "Panel")
                {
                    string[] strInfoSeqNo = new string[2];
                    strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                    string strHdrSeqNo = strInfoSeqNo[0];
                    string strDtlSeqNo = strInfoSeqNo[1];
                    string strSickRecID = mstrSickRecID;
                    string strColTitle = string.Empty;
                    string strResult = string.Empty;
                    string strTitleType = TitleType.SuddenDeafness.ToString();
                    foreach (Control conText in conDtl.Controls)
                    {
                        if (conText.Tag != "" && conText.Tag != null)
                        {
                            strColTitle = conText.Tag.ToString();
                            string strSQL = "select top 1 Result from " + strTableName + " where rowno='" + strColTitle + "' and  ExamSeqNo='" + mstrSeqNo + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and SickRecID='"+ mstrSickRecID  +"' and Type='" + TitleType.SuddenDeafness  + "'";
                            DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                            if (drCheck == null) continue;
                            strResult = CvtFunc.ToString(drCheck["Result"]);
                            if (conText.GetType().Name == "TextBox" )
                            {
                                conText.Text = strResult;
                            }
                            else if (conText.GetType().Name == "DateTimePicker")
                            {
                                if (strResult != "") ((DateTimePicker)conText).Value = CvtFunc.ToDateTime(strResult);
                            }
                            else if (conText.GetType().Name == "RadioButton")
                            {
                                if (strResult == "false")
                                    ((RadioButton)conText).Checked = false;
                                else if (strResult == "true")
                                    ((RadioButton)conText).Checked = true;
                            }
                            else if (conText.GetType().Name == "CheckBox")
                            {
                                if (strResult == "false")
                                    ((CheckBox)conText).Checked = false;
                                else if (strResult == "true")
                                    ((CheckBox)conText).Checked = true;
                            }
                            else if (conText.GetType().Name == "Label")
                            {
                                continue;
                            }
                        }
                    }

                }
            }
            dbcon.CloseCon();
        }

        private void UpdateData(string strExamSeqNo)
        {
            string strTableName = "TreatmentEffectResult";
            DBAccess dbcon = new DBAccess(false, false);
            foreach (Control conDtl in marrlist)
            {
                if (conDtl.GetType().Name == "Panel")
                {
                    string[] strInfoSeqNo = new string[2];
                    strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                    string strHdrSeqNo = strInfoSeqNo[0];
                    string strDtlSeqNo = strInfoSeqNo[1];
                    string strSickRecID = mstrSickRecID;
                    string strColTitle = string.Empty;
                    string strResult = string.Empty;
                    DataRow drRow = null;
                    string strFields = "SickRecID,HdrSeqno,DtlSeqNo,RowNo,ColTitle,Result,Type,ExamSeqNo";
                    foreach (Control conText in conDtl.Controls)
                    {
                        if (conText.Tag != "" && conText.Tag != null)
                        {
                            if (conText.GetType().Name == "TextBox" || conText.GetType().Name == "DateTimePicker")
                            {
                                strResult = conText.Text;
                            }
                            else if (conText.GetType().Name == "DateTimePicker")
                            {
                                strResult = BaseFunctions.FormatDate(((DateTimePicker)conText).Value.ToString (), DateFormatOption.DateFmtYMD);
                            }
                            else if (conText.GetType().Name == "RadioButton")
                            {
                                strResult = (((RadioButton)conText).Checked ? "true" : "false");
                            }
                            else if (conText.GetType().Name == "CheckBox")
                            {
                                strResult = (((CheckBox)conText).Checked ? "true" : "false");
                            }
                            else if (conText.GetType().Name == "Label")
                            {
                                strResult = "";
                                continue;
                            }
                            strColTitle = conText.Tag.ToString();

                            string strSQL = "select top 1 * from " + strTableName + " where rowno='" + strColTitle + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and SickRecID='"+ mstrSickRecID  +"' and ExamSeqNo='"+ strExamSeqNo  +"' and Type='" + TitleType.SuddenDeafness + "'";
                            bool blnIsNew = false;
                            DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                            if (drCheck == null)
                            {
                                blnIsNew = true;
                                drRow = dbcon.GetDataTable("select top 0 * from " + strTableName + " ").NewRow();
                            }
                            else
                            {
                                blnIsNew = false;
                                drRow = dbcon.GetDataFirstRow("select top 1 * from " + strTableName + " where recid='" + CvtFunc.ToString(drCheck["RecID"]) + "' ");
                            }

                            drRow["SickRecID"] = strSickRecID;
                            drRow["HdrSeqno"] = strHdrSeqNo;
                            drRow["DtlSeqNo"] = strDtlSeqNo;
                            drRow["RowNo"] = strColTitle;
                            drRow["Result"] = strResult;
                            drRow["Type"] = TitleType.SuddenDeafness ;
                            drRow["ExamSeqNo"] = strExamSeqNo;
                            if (blnIsNew)
                                DataFunctions.UpdateTable(dbcon, strFields, strTableName, drRow, FormStatus.AddNew);
                            else
                                DataFunctions.UpdateTable(dbcon, strFields, strTableName, drRow, FormStatus.Update);
                        }
                    }
                }
            }
            dbcon.CloseCon();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if ( UpdateHdr ()) this.Close(); 
        }

        private bool UpdateHdr()
        {
            string strSeqNo = string.Empty;
            DBAccess dbcon = new DBAccess(false, false);
            string strSQL = string.Empty;
            string strCaseName = string.Empty;
            string strThrombolysis = string.Empty;
            strSQL = "select top 1 * from mTreatHistory where SickRecID='" + mstrSickRecID + "' order by RecID desc";
            DataRow drCaseName = dbcon.GetDataFirstRow(strSQL);
            if (drCaseName != null)
            {
                strCaseName = CvtFunc.ToString(drCaseName["TreatCaseName"]);
                strThrombolysis = CvtFunc.ToString(drCaseName["Thrombolysis"]);
            }

                strSQL = " if not exists ( select * from TreatmentEffectHdr where SeqNo='" + mstrSeqNo + "' and Type='" + TitleType.SuddenDeafness + "' and SickRecID='" + mstrSickRecID + "')";
                strSeqNo = "3";
                strSQL += " Insert into TreatmentEffectHdr(  SickRecID, SeqNo, ExamDate,Type,Remark,CaseName,Thrombolysis ) " +
                         " values (  '" + mstrSickRecID + "','" + strSeqNo + "','" + BaseFunctions.FormatDate(DateTime.Now.Date.ToString(), DateFormatOption.DateFmtYMD) + "' " +
                         " ,'" + TitleType.SuddenDeafness + "','"+ SysVar.CurrentMenuInfo.strMenuName +"', '"+ strCaseName +"','"+ strThrombolysis +"' ) ";
                dbcon.ExecuteSql(strSQL);
                mstrSeqNo = strSeqNo;
          
            UpdateData(strSeqNo);
            mfrmStatus = FormStatus.Update;
            return true;
        }

        private void frmSDTreatmentEffectTitle_Load(object sender, EventArgs e)
        {
            if (DataFunctions.GetFieldValue("mTreatHistory", "TreatCaseName", " SickRecID='" + mstrSickRecID + "'") != "")
                FillData();
            else
            {
                this.Close();
                MessageBox.Show("请先选择 治疗方案", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            this.timer1.Start();
        }

        private void ubtJacobson_Click(object sender, EventArgs e)
        {
            if (UpdateHdr())
            {
                if (mstrSeqNo == "") return;
                frmSDJacobsonTitle frmJacob = new frmSDJacobsonTitle(mstrSickRecID, mstrSeqNo);
                frmJacob.ShowDialog();
            }
        }

        private void btnJacobson_Click(object sender, EventArgs e)
        {
            if (!UpdateHdr()) return;
            frmSDJacobsonTitle frmTitle = new frmSDJacobsonTitle(mstrSickRecID, mstrSeqNo);
            double dblFscore = 0;
            double dblEscore = 0;
            double dblPscore = 0;
            double dblTotalscore = 0;
            double dblSubScore = 0;
            if (frmTitle.GetTotalJacobson(ref dblTotalscore, ref dblFscore, ref dblEscore, ref dblPscore, ref dblSubScore))                     
            txtTotalScore.Text = CvtFunc.ToString(dblTotalscore); 
        }

        private void frmSDTreatmentEffectTitle_28_FormClosed(object sender, FormClosedEventArgs e)
        {
            SysVar.marrFormList.Remove(this.Name);
            frmGifImage frmGIF = new frmGifImage();
            frmGIF.TopMost = true;
            frmGIF.Show();
        }


        private void SelectNextText(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
                SelectNextControl((Control)sender, true, true, true, true);
        }

        private void textBox15_TextChanged(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 1)
                timer1.Stop();
            this.Opacity += 0.04;
        }

        private void btnCalTHI_Click(object sender, EventArgs e)
        {
            if (!UpdateHdr()) return;
            frmSDTHITitle frmTitle = new frmSDTHITitle(mstrSickRecID, mstrSeqNo);
            double dblFscore = 0;
            double dblEscore = 0;
            double dblPscore = 0;
            double dblTotalscore = 0;
            double dblSubScore = 0;
            if (frmTitle.GetTotalJacobson(ref dblTotalscore, ref dblFscore, ref dblEscore, ref dblPscore, ref dblSubScore))
                txtTHITotalscore.Text  = CvtFunc.ToString(dblTotalscore); 
        }
    }
}
